package com.ruoyi.factorydata.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.factorydata.domain.EquipmentData;
import com.ruoyi.factorydata.domain.EquipmentType;
import com.ruoyi.factorydata.domain.ProductionLineData;
import com.ruoyi.factorydata.domain.StationData;
import com.ruoyi.factorydata.domain.vo.EquipmentDataVo;
import com.ruoyi.factorydata.mapper.EquipmentDataMapper;
import com.ruoyi.factorydata.mapper.EquipmentTypeMapper;
import com.ruoyi.factorydata.mapper.ProductionLineDataMapper;
import com.ruoyi.factorydata.mapper.StationDataMapper;
import com.ruoyi.factorydata.service.IEquipmentDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 设备信息数据Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-04
 */
@Service
public class EquipmentDataServiceImpl extends ServiceImpl<EquipmentDataMapper, EquipmentData> implements IEquipmentDataService {
    @Autowired
    private EquipmentDataMapper equipmentDataMapper;

    @Autowired
    private ProductionLineDataMapper productionLineDataMapper;

    @Autowired
    private StationDataMapper stationDataMapper;

    @Autowired
    private EquipmentTypeMapper equipmentTypeMapper;

    /**
     * 查询设备信息数据
     *
     * @param id 设备信息数据主键
     * @return 设备信息数据
     */
    @Override
    public EquipmentData selectEquipmentDataById(Long id) {
        return equipmentDataMapper.selectEquipmentDataById(id);
    }

    /**
     * 查询设备信息数据列表
     *
     * @param equipmentData 设备信息数据
     * @return 设备信息数据
     */
    @Override
    public List<EquipmentDataVo> selectEquipmentDataList(EquipmentData equipmentData) {
        List<EquipmentData> equipmentDataList = equipmentDataMapper.selectEquipmentDataList(equipmentData);
        List<EquipmentDataVo> equipmentDataVos = equipmentDataList.stream().map(item -> {
            ProductionLineData productionLineData = productionLineDataMapper.selectById(item.getProductionLineId());
            EquipmentType equipmentType = equipmentTypeMapper.selectById(item.getTypeId());
            StationData stationData = new StationData();
            if(item.getStationId() != null){
                stationData = stationDataMapper.selectById(item.getStationId());
            }

            return EquipmentDataVo.builder()
                    .id(item.getId())
                    .equipmentCode(item.getEquipmentCode())
                    .equipmentName(item.getEquipmentName())
                    .typeId(item.getTypeId())
                    .typeName(equipmentType != null ? equipmentType.getTypeName() : "")
                    .model(item.getModel())
                    .serialNo(item.getSerialNo())
                    .equipmentStatus(item.getEquipmentStatus())
                    .productionLineId(item.getProductionLineId())
                    .productionLineName(productionLineData != null ? productionLineData.getName() : "")
                    .stationId(item.getStationId())
                    .stationName(stationData != null ? stationData.getName() : "")
                    .purchaseDate(item.getPurchaseDate())
                    .manufacturer(item.getManufacturer())
                    .purchasePrice(item.getPurchasePrice())
                    .equipmentImages(item.getEquipmentImages())
                    .equipmentAttachments(item.getEquipmentAttachments())
                    .remark(item.getRemark())
                    .createBy(item.getCreateBy())
                    .createTime(item.getCreateTime())
                    .updateTime(item.getUpdateTime())
                    .build();
        }).collect(Collectors.toList());
        Page<EquipmentData> queryData=(Page<EquipmentData>)equipmentDataList;
        Page<EquipmentDataVo> page = new Page<>(queryData.getPageNum(), queryData.getPageSize());
        page.setTotal(queryData.getTotal());
        page.addAll(equipmentDataVos);
        return page;
    }

    /**
     * 新增设备信息数据
     *
     * @param equipmentData 设备信息数据
     * @return 结果
     */
    @Override
    public int insertEquipmentData(EquipmentData equipmentData) {
                equipmentData.setCreateTime(DateUtils.getNowDate());
            return equipmentDataMapper.insertEquipmentData(equipmentData);
    }

    /**
     * 修改设备信息数据
     *
     * @param equipmentData 设备信息数据
     * @return 结果
     */
    @Override
    public int updateEquipmentData(EquipmentData equipmentData) {
                equipmentData.setUpdateTime(DateUtils.getNowDate());
        return equipmentDataMapper.updateEquipmentData(equipmentData);
    }

    /**
     * 批量删除设备信息数据
     *
     * @param ids 需要删除的设备信息数据主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentDataByIds(Long[] ids) {
        int i = equipmentDataMapper.deleteBatchIds(Arrays.stream(ids).collect(Collectors.toList()));
        return i;
    }

    /**
     * 删除设备信息数据信息
     *
     * @param id 设备信息数据主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentDataById(Long id) {
        return equipmentDataMapper.deleteEquipmentDataById(id);
    }
}